<!DOCTYPE html>
<html>
<head>
  <title>HTML5 Geo-location test</title>
  <script type="text/javascript" src="../test_bootstrap.js"></script>
  <script type="text/javascript">
    goog.require('bot');
    goog.require('bot.geolocation');
    goog.require('bot.test');
    goog.require('bot.html5');
    goog.require('bot.userAgent');
    goog.require('goog.testing.AsyncTestCase');
    goog.require('goog.testing.jsunit');
    goog.require('goog.userAgent');
    goog.require('goog.userAgent.product');
  </script>

  <script type="text/javascript">
    var asyncTestCase = null;

    // WebDriver does not enable geolocation for Android (b/5787618).
    // WebDriver does not enable geolocation for Chrome (b/5416356).
    // WebDriver does not enable geolocation for Firefox (b/5787180).
    // WebDriver does not enable geolocation for Opera 11.5 (b/5746540).
    // WebDriver does not enable geolocation for IE 9 (b/5817463).
    // Selenium breaks geolocation on Opera (b/5773581).
    var GEOLOCATION_NOT_WORKING =
      !bot.html5.isSupported(bot.html5.API.GEOLOCATION) ||
      goog.userAgent.product.ANDROID ||
      goog.userAgent.product.CHROME ||
      goog.userAgent.product.FIREFOX ||
      bot.userAgent.IE_DOC_9 ||
      (goog.userAgent.OPERA && (bot.test.isSeleniumBacked() ||
                                bot.userAgent.isEngineVersion(11.5)));


    /**
     * This method checks if the device location
     * can be retrieved, i.e. non-null value of Position within the timeout
     * period.
     */
    function testLocationWithinDefaultInterval() {
      if (GEOLOCATION_NOT_WORKING) {
        return;
      }

      function onSuccess(position) {
        asyncTestCase.continueTesting();
        //5s (default timeout) should definitely be sufficient
        //to retrieve a position.
      }

      function onError(error) {
        asyncTestCase.continueTesting();
        fail('When enabled, location should be known within 10s');
      }

      try {
        bot.geolocation.getCurrentPosition(onSuccess, onError);
        asyncTestCase.waitForAsync('geolocation.getCurrentPosition');
      } catch (e) {
        assertEquals(e.code, bot.ErrorCode.UNKNOWN_ERROR);
      }
    }

    /**
     * Tested with Chrome and Firefox. It checks if the device location cannot
     * can be retrieved, i.e. null value of Position, when requested to
     * retrieve the position with high accuracy and immediately (no cached).
     * Note: Firefox seems to return the position even in this case.
     * The HTML5 standard states If the value of the timeout variable is 0,
     * invoke the errorCallback (if present) with a new PositionError object
     * whose code attribute is set to TIMEOUT.
     */
    function testLocationNoTimeout() {
      if (GEOLOCATION_NOT_WORKING) {
        return;
      }

      var posOptions = {
        enableHighAccuracy: true,
        maximumAge: 0,
        timeout: 0
      };

      function onSuccess(position) {
        asyncTestCase.continueTesting();
        fail('Location within 0s timeout interval and 0s max age fails');
      }

      function onError(error) {
        asyncTestCase.continueTesting();
      }

      try {
        bot.geolocation.getCurrentPosition(onSuccess, onError, posOptions);
        asyncTestCase.waitForAsync('geolocation.getCurrentPosition');
      } catch (e) {
        assertEquals(e.code, bot.ErrorCode.UNKNOWN_ERROR);
      }
    }

    // TODO(user): Add more tests to check the returned value.
  </script>
</head>
<body>
  <script type="text/javascript">
    asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
    asyncTestCase.stepTimeout = 15 * 1000;
  </script>
</body>
</html>
